package processing.test.mechaapp;

import processing.core.*;

public class Timer {
	
	// The direction (up or down) the timer will count
	public static final int COUNTUP = 0;
	public static int COUNTDOWN = 1;

	public static PApplet applet;

	//Timer variables, 
	public int direction = COUNTUP; // Default is COUNTUP
	public long start;
	public long curTime;
	public long limit;
	public boolean running;
	int xPos, yPos;

	//Constructor which sets the time limit, and the position of timer(xchord, ychord)
	public Timer(int lim, int x, int y) {
		limit = lim;
		xPos = x;
		yPos = y;
		running = false;
		curTime = 0;
	}
	
	//method which sets the direction of time. Either counting up or counting down.
	public void setDirection(int dir) {
		direction = dir;
	}
	
	//method that calculates the time going up or down.
	public int getTime() {
		//if direction is count up do calculations
		if (direction == COUNTUP) {
			if (running) {
				return ((int) ((applet.millis() - start) / 1000.0));
			} else {
				return ((int) (curTime / 1000.0));
			}
			//if directions is count down
		} else {
			if (running) {
				return ((int) ((limit - (applet.millis() - start)) / 1000.0));
			} else {
				return ((int) (curTime / 1000.0));
			}
		}
	}
	
	//method adding extra seconds to the timer.
	//(extra point when creating complete molecules)
	public void addtime(int addtime) {
		if (direction == COUNTDOWN) {
			limit += addtime;
		}
	}

	//method to start the timer.
	public void start(int lim) {
		limit = lim;
		running = true;
		start = applet.millis();
	}

	//method to pause time when the paused button is pressed. 
	public void pause() {
		if (running) {
			curTime = applet.millis() - start;
			running = false;
		}
	}

	//method to resume the time when game is unpaused.
	public void resume() {
		if (!running) {
			start = applet.millis() - curTime;
			running = true;
		}
	}
	//method displaying the time.
	public void display() {
		int seconds, minutes;
		String output = "";
		String secondsStr = "";

		seconds = getTime();
		minutes = seconds / 60;

		if ((seconds % 60) < 10 && minutes > 0) {
			secondsStr = "0" + (seconds % 60);
		} else {
			secondsStr = secondsStr + (seconds % 60);
		}

		if (minutes > 0) {
			output = output + minutes + ":" + secondsStr;
		} else {
			output = output + secondsStr;
		}
		//setting time to a certain font and position
		PFont font;
		font = applet.loadFont("Roboto-Bold-48.vlw");
		applet.fill(255);
		applet.textFont(font);
		applet.textAlign(PConstants.CENTER);
		applet.text(output, xPos, yPos);
	}

	// needed to set the attributes of the current applet.
	public static void setApplet(PApplet frame) {
		applet = frame;
	}
	
	public String toString() {
		int seconds, minutes;
		String output = "";
		String secondsStr = "";

		seconds = getTime();
		minutes = seconds / 60;

		if ((seconds % 60) < 10 && minutes > 0) {
			secondsStr = "0" + (seconds % 60);
		} else {
			secondsStr = secondsStr + (seconds % 60);
		}

		if (minutes > 0) {
			output = output + minutes + ":" + secondsStr;
			return output;
		} else {
			output = output + secondsStr;
			return output;
		}
	}
}
